package com.interview.a_多线程.c_停止线程.c_迭代后阻塞_停止线程;

/**
 * 迭代时阻塞 停止线程
 * <p>
 * 通过 tray catch 进行中断
 * 迭代时存在阻塞情况时，不需要在循环条件中添加 是否终端状态判断
 * <p>
 * run有sleep时停止线程
 */
public class StopThread implements Runnable {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new StopThread());
        thread.start();

        //执行0.5秒时中断
        Thread.sleep(3000);
        thread.interrupt();
    }

    @Override
    public void run() {
        int count = 0;
        try {
            System.out.println("Thread.currentThread().isInterrupted()---> " + Thread.currentThread().isInterrupted());
            while (count <= 1000) {
                if (count % 10 == 0) {
                    System.out.println(count + "  是10的倍数");
                }
                count++;
                Thread.sleep(100);
            }
            System.out.println("任务运行结束...  当前 interrupt=" + Thread.currentThread().isInterrupted());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
